package jsmc.opendata.pratikanant;

import jsmc.android.object.DialogButton;
import jsmc.android.object.NeoDialog;
import jsmc.opendata.nantes.util.NeoLog;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;
import greendroid.app.GDActivity;

public class MainGDActivity extends GDActivity{
	
	protected static final int RETURN_SPLASH = 1000;
	protected static final int RETURN_DIALER = 1001;
	protected static final int RETURN_LOGIN  = 1002;
	
	protected static final int RETURN_CONTACTS     = 1003;
	protected static final int RETURN_PREFERENCES  = 1004;
	protected static final int RETURN_INFOSLEGALES = 1005;
	
	protected MainApp app = null;
	protected ProgressDialog dialog = null; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        app = (MainApp) getApplicationContext();
        
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
    
	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {

		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {

		super.onRestoreInstanceState(savedInstanceState);
	}
	protected void CreateDialog(String title, String message,final DialogButton PositiveButton,final DialogButton NegativeButton){

    	NeoLog.v("LB MainActivity","Dialog "+title);
    	
        Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(message);
              
        if(PositiveButton!=null)
	        builder.setPositiveButton(PositiveButton.GetLib(), new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	            	dialog.cancel();
	            	Intent intent = PositiveButton.GetIntent();
	            	if(intent!=null){
		            	try {
		            		NeoLog.v("LB MainActivity","DialogFinish start "+intent.getAction());
		            		startActivity(intent);
		            	} catch (ActivityNotFoundException e) {
							// TODO: handle exception
						}
	            	}
	            	
	            	if(PositiveButton.Finish())
	            		finish();
	            }
	        });
        
        
		if(NegativeButton!=null)
		    builder.setNegativeButton(NegativeButton.GetLib(), new DialogInterface.OnClickListener() {
		          public void onClick(DialogInterface dialog, int whichButton) {
		          	dialog.cancel();
		          	Intent intent = NegativeButton.GetIntent();
		          	if(intent!=null){
			            try {
			            	NeoLog.v("LB MainActivity","DialogFinish start "+intent.getAction());
		            		startActivity(intent);
		            	} catch (ActivityNotFoundException e) {
							// TODO: handle exception
						}
		      	}
		      	if(NegativeButton.Finish())
		      		finish();
		      }
		   });
  
        AlertDialog dialog = builder.show();
        TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
        messageView.setGravity(Gravity.CENTER);
        
	}
	
	protected void CreateNeoDialog(final NeoDialog neoDialog){

		String title = neoDialog.getTitle();
		String message = neoDialog.getMessage();
        
    	NeoLog.v("LB MainActivity","Dialog "+title);
    	
        Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(message);

        
        if(neoDialog.getPositiveButton()!=null){
        	NeoLog.v("LB MainActivity","CreateNeoDialog PositiveButton");
	        builder.setPositiveButton(neoDialog.getPositiveButton().GetLib(), new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	            	dialog.cancel();
	            	Intent intent = neoDialog.getPositiveButton().GetIntent();
	            	if(intent!=null){
		            	try {
		            		NeoLog.v("LB MainActivity","DialogFinish start "+intent.getAction());
		            		startActivity(intent);
		            	} catch (ActivityNotFoundException e) {
							// TODO: handle exception
						}
	            	}
	            	if(neoDialog.getPositiveButton().Finish()){
	            		NeoLog.v("LB MainActivity","Dialog finish");
	            		finish();
	            	}
	            	
	            	OnCloseDialog();
	            }
	        });
        }
        
        
        
		if(neoDialog.getNegativeButton()!=null){
			NeoLog.v("LB MainActivity","CreateNeoDialog NegativeButton");
		    builder.setNegativeButton(neoDialog.getNegativeButton().GetLib(), new DialogInterface.OnClickListener() {
		          public void onClick(DialogInterface dialog, int whichButton) {
			          	dialog.cancel();
			          	Intent intent = neoDialog.getNegativeButton().GetIntent();
			          	if(intent!=null){
				            try {
				            	NeoLog.v("LB MainActivity","DialogFinish start "+intent.getAction());
			            		startActivity(intent);
			            	} catch (ActivityNotFoundException e) {
								// TODO: handle exception
							}
				      	}
				      	if(neoDialog.getNegativeButton().Finish())
				      		finish();
				      	
				      	OnCloseDialog();
			     }
		   });
		}
  
        AlertDialog dialog = builder.show();
        TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
        messageView.setGravity(Gravity.CENTER);
        
	}	
	

	public void ShowProgress(String message){
		
        dialog = ProgressDialog.show(this, "",  message , true);
        dialog.show();
        
	}
	
	public void CloseProgress(){
        
        if(dialog!=null){
        	dialog.dismiss();
        	dialog = null;
        }
    }
    
	public void Wait(long time){
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void Stop() {
		// TODO Auto-generated method stub
		
	}
	

	public void OnCloseDialog(){
		
	}
 
}
